package com.yanqu.road.dao.impl.activity.ruishou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ruishou.RuiShouUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class RuiShouUserDataDaoImpl extends TempDataObjectDao<RuiShouUserData> {

    public static String getRuiShouUserDataSql = "select * from `t_u_activity_rui_shou_user_data` where `activityId`=? and `userId`=?";

    public RuiShouUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getRuiShouUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertRuiShouUserDataSql = "insert into `t_u_activity_rui_shou_user_data` (activityId,userId,killMonsterCntMap,score) values (?,?,?,?)";

    @Override
    protected boolean addData(RuiShouUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getKillMonsterCntMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        if (getDb().executeNoneQuery(insertRuiShouUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateRuiShouUserDataSql = "update `t_u_activity_rui_shou_user_data` set `killMonsterCntMap`=?,`score`=? where `activityId`=? and `userId`=?";

    @Override
    protected boolean updateData(RuiShouUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getKillMonsterCntMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateRuiShouUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected RuiShouUserData parseData(ResultSet rs) throws SQLException {
        RuiShouUserData data = new RuiShouUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setScore(rs.getLong("score"));
        String killMonsterCntMap = rs.getString("killMonsterCntMap");
        if (!StringUtils.isNullOrEmpty(killMonsterCntMap)) {
            data.setKillMonsterCntMap(JSON.parseObject(killMonsterCntMap, new TypeReference<Map<Integer, Integer>>() {
            }));
        }
        return data;
    }
}
